﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace BloodBot
{
    public class Map : Microsoft.Xna.Framework.GameComponent
    {
        public List<Cell> Cells;
        public int AmountOfRowsCols;
        public float cellSize;
        public float floorDepth;
        public int ID;
        
        public string name;
        
        public Map(Game game, int AmountOfRowsCols, float cellSize, float floor, int ID, string name) :base(game)
        {
            this.AmountOfRowsCols = AmountOfRowsCols;
            this.cellSize = cellSize;
            this.floorDepth = floor;
            this.ID = ID;
            this.name = name;
            Cells = new List<Cell>();
            
            buildMap(game);
        }

        private void loadMap()
        {
            //here we will load from an XML
            //but for now we will make the map by hand
            
        }

        public void buildMap(Game game)
        {
            float x, y, z;
            float midpoint = ((AmountOfRowsCols * cellSize) / 2 - 0.5f);
            y = floorDepth;

            for (int row = 0; row < AmountOfRowsCols; row++)
            {
                for (int col = 0; col < AmountOfRowsCols; col++)
                {
                    // going to put the centermap code in here
                    x = row * cellSize - midpoint;
                    z = col * cellSize - midpoint;

                    Cells.Add(new Cell(game, row, col, x, y, z, "Ground", "Solid", cellSize));
                }
            }

        }

        public void SetSomeWallsUp(int level)
        {
            //first, reset the level to all ground cells
            foreach (Cell i in Cells)
            {
                i.setType("Ground");
            }

            if (level == 1)
            {
                foreach (Cell i in Cells)
                {
                    //cross 1
                    if (i.row == 10 && i.col <= 13 && i.col >= 7)
                        i.setType("Wall");
                    if (i.col == 10 && i.row <= 13 && i.row >= 7)
                        i.setType("Wall");
                    //cross 2
                    if (i.row == 21 && i.col <= 24 && i.col >= 18)
                        i.setType("Wall");
                    if (i.col == 21 && i.row <= 24 && i.row >= 18)
                        i.setType("Wall");
                }
            }
             
            if (level == 2)
            {
                foreach (Cell i in Cells)
                {
                    //BOMBERMAN SYLE
                    if ((i.row % 2) == 1 && (i.col % 2) == 1)
                    {
                        i.setType("Wall");
                    }
                    if (i.row > 14 && i.row < 16 && i.col > 14 && i.row < 16)
                        i.setType("Ground");
                }
            }

            if (level == 3)
            {
                foreach (Cell i in Cells)
                {
                    // Just a line
                    if ((i.row) == 10 && i.col >= 5 && i.col <= 20)
                    {
                        i.setType("Wall");
                    }

                    if ((i.col) == 10 && i.row >= 5 && i.row <= 20)
                    {
                        i.setType("Wall");
                    }
                }
            }

        }

        public string allCellsToString()
        {
            string s = "";
            foreach (Cell i in Cells)
            {
                s += i.customToString() + "\n";
            }
            return s;
        }

        public void DrawAllCells(Camera camera, bool debug)
        {
            foreach (Cell i in Cells)
            {
                i.Draw(camera, debug);
            }
        }

        public void TrimCellsOutsideBoundary(float boundaryRadius)
        {
            
            foreach (Cell i in Cells)
            {
                if (Vector3.Distance(new Vector3(0, 0, 0), i.pos)
                    > Vector3.Distance(new Vector3(0, 0, 0), new Vector3(boundaryRadius, 0, 0)))
                    i.Disable();
            }
        }

        public List<BoundingBox> GetBoundingBoxList()
        {
            List<BoundingBox> CellBoundingBoxes;
            CellBoundingBoxes = new List<BoundingBox>();
            
            foreach (Cell i in Cells)
            {
                CellBoundingBoxes.Add(i.getBoundingBox());
            }

            return CellBoundingBoxes;
        }

        public Cell VectorToCell(Vector3 location)
        {
            foreach (Cell cell in Cells)
            {
                if (cell.ContainsVector(location))
                {
                    return cell;
                }
            }
            return null;
        }

        public List<Cell> GetCells()
        {
            return Cells;
        }

        public List<Cell> getWalls()
        {
            List<Cell> wallCells = new List<Cell>();
            foreach (Cell i in Cells)
            {
                if (i.type == "Wall")
                    wallCells.Add(i);
            }
            return wallCells;
        }
    }
}
